/**
 * 管理msite功能模块的相关状态的vuex数据
 */

import {
  reqAddress,
  reqFoodsList,
  reqShopsList,
} from '../../api'

import {
  RECEIVE_ADDRESS,
  RECEIVE_FOODSLIST,
  RECEIVE_SHOPSLIST,
} from '../mutations-types'

const state = {
  latitude: '40.10038', //纬度
  longitude: '116.36867', //经度
  address: {},
  foodsList: [],
  shopsList: [],
}
const mutations = {
  [RECEIVE_ADDRESS](state, address) {
    state.address = address
  },

  [RECEIVE_FOODSLIST](state, foodsList) {
    state.foodsList = foodsList
  },

  [RECEIVE_SHOPSLIST](state, shopsList) {
    state.shopsList = shopsList
  },
}

const actions = {
  // 获取地址
  async getAddress({
    commit,
    state
  }) {
    // 调用接口请求函数
    const geohashe = state.latitude + ',' + state.longitude
    const result = await reqAddress(geohashe)
    // 判断请求成功
    if (result.code === 0) {
      const address = result.data
      // 提交到mutations中
      commit(RECEIVE_ADDRESS, address)
    }
  },

  // 获取轮播图列表
  async getFoodsList({
    commit
  }) {
    const result = await reqFoodsList()
    if (result.code === 0) {
      commit(RECEIVE_FOODSLIST, result.data)
    }
  },

  // 获取商店店铺列表
  async getShopsList({
    commit,
    state
  }) {
    const result = await reqShopsList(state.latitude, state.longitude)
    if (result.code === 0) {
      commit(RECEIVE_SHOPSLIST, result.data)
    }
  },
}
const getters = {}

export default {
  state,
  mutations,
  actions,
  getters
}
